package plgutils

import (
	"runtime"
	"sync/atomic"
	"time"
)

type ObjStorageSession struct {
	SessionId   string
	ObjectID    string
	RequestData string
	PingData    string
	Match       string
	RequestUser string
	StartTime   time.Time
	LastTime    time.Time
}

func (this *ObjStoragePlg) newSession(sessid string) *ObjStorageSession {
	rval := &ObjStorageSession{SessionId: sessid}
	atomic.AddInt32(&this.sess_obj_cnt, 1)
	runtime.SetFinalizer(rval, func(obj interface{}) {
		atomic.AddInt32(&this.sess_obj_cnt, -1)
	})
	return rval
}

func (this *ObjStoragePlg) checkGetSession(sessid string, newflag bool) *ObjStorageSession {
	if !newflag {
		obj := this.sesslst.Get(sessid, (*ObjStorageSession)(nil))
		if obj == nil {
			return nil
		}
		return obj.(*ObjStorageSession)
	} else {
		rval := this.sesslst.LoadOrStoreFunc(sessid, func() interface{} {
			return this.newSession(sessid)
		}, nil).(*ObjStorageSession)
		return rval
	}
}
